package cn.darxs.noquestion.controller;

import cn.darxs.noquestion.common.BaseContext;
import cn.darxs.noquestion.common.Response;
import cn.darxs.noquestion.entity.*;
import cn.darxs.noquestion.service.AdminService;
import cn.darxs.noquestion.service.RenterService;
import cn.darxs.noquestion.service.ReplierService;
import cn.darxs.noquestion.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import static cn.darxs.noquestion.common.ResponseStateCode.*;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    AdminService adminService;
    @Autowired
    RenterService renterService;
    @Autowired
    ReplierService replierService;
    @Autowired
    UserService userService;

    @PostMapping   //post请求这样写。。其他请求请参考resful风格
    public Response login(HttpServletRequest request, @RequestBody JSONObject params, HttpServletResponse response){

        String userName = params.get("userName").toString();   //前端传过来一个json格式
        String passWord = params.get("passWord").toString();

        Response httpResponseEntity = new Response();

        LambdaQueryWrapper<Admin> adminWrapper = new LambdaQueryWrapper<>();
        adminWrapper.eq(Admin::getUsername,userName);
        adminWrapper.eq(Admin::getPassword,passWord);
        Admin admin = adminService.getOne(adminWrapper);

        LambdaQueryWrapper<Renter> renterWrapper = new LambdaQueryWrapper<>();
        renterWrapper.eq(Renter::getUsername,userName);
        renterWrapper.eq(Renter::getPassword,passWord);
        Renter renter = renterService.getOne(renterWrapper);

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername,userName);
        userWrapper.eq(User::getPassword,passWord);
        User user = userService.getOne(userWrapper);

        LambdaQueryWrapper<Replier> replierWrapper = new LambdaQueryWrapper<>();
        replierWrapper.eq(Replier::getUsername,userName);
        replierWrapper.eq(Replier::getPassword,passWord);
        Replier replier = replierService.getOne(replierWrapper);

        if(admin != null){
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("admin");
            httpResponseEntity.setData(admin);
            //将登陆admin的id设置到session中
            request.getSession().setAttribute("userId",admin.getId());
            request.getSession().setAttribute("status","admin");
            BaseContext.setCurrentId(admin.getId());
            log.info("sessionid:{}", BaseContext.getCurrentId());
            try {
                response.addCookie(new Cookie("name", URLEncoder.encode(admin.getName(),"UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else if(renter != null){
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("renter");
            httpResponseEntity.setData(renter);
            //将登陆租户的id设置到session中
            request.getSession().setAttribute("userId",renter.getId());
            request.getSession().setAttribute("status","renter");
            BaseContext.setCurrentId(renter.getId());
            log.info("sessionid:{}", BaseContext.getCurrentId());
            try {
                response.addCookie(new Cookie("name", URLEncoder.encode(renter.getName(),"UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else if(user != null){
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("user");
            httpResponseEntity.setData(user);
            //将登陆用户的id设置到session中
            request.getSession().setAttribute("userId",user.getId());
            request.getSession().setAttribute("status","user");
            BaseContext.setCurrentId(user.getId());
            log.info("sessionid:{}", BaseContext.getCurrentId());
            try {
                response.addCookie(new Cookie("name", URLEncoder.encode(user.getName(),"UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else if(replier != null){
            httpResponseEntity.setCode(OPERATION_SUCCEED); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("replier");
            httpResponseEntity.setData(replier);
            //将登陆答者的id设置到session中
            request.getSession().setAttribute("userId",replier.getId());
            request.getSession().setAttribute("status","replier");
            BaseContext.setCurrentId(replier.getId());
            log.info("sessionid:{}", BaseContext.getCurrentId());
            try {
                response.addCookie(new Cookie("name", URLEncoder.encode(replier.getName(),"UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else{
            httpResponseEntity.setCode(NO_LOGIN); //回来统一这些code 和 Message
            httpResponseEntity.setMessage("444");
        }
        return httpResponseEntity;
    }
}
